package com.flame.user.controller;

import java.util.List;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.auth.core.context.AuthContextHolder;
import com.flame.auth.core.entity.TrustedPrincipal;
import com.flame.auth.core.exception.AuthExceptionMessage;
import com.flame.auth.core.exception.AuthRuntimeException;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.user.controller.vo.UserInfoVo;
import com.flame.user.controller.vo.UserRoleAddVo;
import com.flame.user.controller.vo.UserRoleVo;
import com.flame.user.entity.UserInfo;
import com.flame.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * flame_user_info 用户信息表
 *
 * @author 吴欣童
 * @since 2022-12-14 17:20:40
 */
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/user")
    public Result user(@RequestBody @Validated UserInfo userInfo) {
        return Result.success(userInfoService.findById(userInfo.getUserId()));
    }

    @PostMapping("/user/simple/list")
    public Result userSimple(@RequestBody List<String> userIdList) {
        return Result.success(userInfoService.listSampleUserByIds(userIdList));
    }

    @PostMapping("/user/simple/page")
    public Result list(@RequestBody UserInfoVo userInfoVo) {
        return Result.success(userInfoService.simpleList(userInfoVo));
    }

    @HasAuth("user")
    @PostMapping("/user/page")
    public PageResult pageUserInfo(@RequestBody UserInfoVo userInfoVo) {
        return PageResult.success(userInfoService.pageUserInfo(userInfoVo));
    }

    @HasAuth("user:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/add")
    public Result addUserInfo(@RequestBody @Validated UserInfo userInfo) {
        userInfoService.addUserInfo(userInfo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/update")
    public Result updateUserInfo(@RequestBody @Validated UserInfo userInfo) {
        TrustedPrincipal trustedPrincipal = AuthContextHolder.getContext();
        if (trustedPrincipal.getAccount().equals(userInfo.getUserId()) || trustedPrincipal.hasAuth("user:edit")) {
            userInfoService.updateUserInfo(userInfo);
        } else {
            throw new AuthRuntimeException(AuthExceptionMessage.ACCOUNT_NO_AUTH);
        }
        return Result.success();
    }

    @HasAuth("user:role")
    @PostMapping("/user/role/page")
    public Result pageUserRole(@RequestBody @Validated UserRoleVo userRoleVo) {
        return Result.success(userInfoService.pageUserRole(userRoleVo));
    }

    @HasAuth("user:role")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/role/add")
    public Result addUserRole(@Validated @RequestBody UserRoleAddVo userRoleAddVo) {
        userInfoService.addUserRole(userRoleAddVo);
        return Result.success();
    }
}
